package com.liyunc.demo.base.component;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;

public class JRadioButtonExample {

    // 创建两个单选按钮
    JRadioButton radioBtn01 = new JRadioButton("男");
    JRadioButton radioBtn02 = new JRadioButton("女");
    JFrame jf = new JFrame("测试窗口");
    JPanel panel = new JPanel();
    public void test() {

        jf.setSize(200, 200);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 创建按钮组，把两个单选按钮添加到该组
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(radioBtn01);
        btnGroup.add(radioBtn02);

        // 设置第一个单选按钮选中
        radioBtn01.setSelected(true);

        radioBtn01.addActionListener(event -> {
            System.out.println("jjjjjjjjj");
        });
        radioBtn02.addActionListener(event -> {
            System.out.println("kkkkkkkkk");
        });

        panel.add(radioBtn01);
        panel.add(radioBtn02);

        jf.setContentPane(panel);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        JRadioButtonExample jRadioButtonExample = new JRadioButtonExample();
        jRadioButtonExample.test();
    }


}
